package com.langsin.crmpro.manager;


import static com.langsin.crmpro.utils.STool.$;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.langsin.crmpro.base.BaseManager;
import com.langsin.crmpro.dao.DictionaryDetailDAO;
import com.langsin.crmpro.model.SDictionaryClass;
import com.langsin.crmpro.model.SDictionaryDetail;

public class DictionaryDetailManagerImpl extends BaseManager implements DictionaryDetailManager
{
	private DictionaryDetailDAO dictionaryDetailDAO;
	
	
	public void setDictionaryDetailDAO(DictionaryDetailDAO dictionaryDetailDAO) 
	{
		this.dictionaryDetailDAO = dictionaryDetailDAO;
	}


	public void updateDictionaryDetails(String _oldvalue, String _newvalue, String _code) 
	{
		String[] $ov = _oldvalue.split("%#@");
		
		for( int i=0; i< $ov.length; i++)
		{
			String[] $ov_ = $ov[i].split("@#%");
			if($ov_.length == 2)
			{
				dictionaryDetailDAO.updateDictionaryDetails($ov_[0], $ov_[1]);
				
				log.debug($ov_[0]+"-"+$ov_[1]);
			}
		}
		
		if($(_newvalue))
		{
			//取得当前的pid
			BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext-*.xml");
			DictionaryManager dm = (DictionaryManager)bf.getBean("dictionaryManager");
			SDictionaryClass sDictionaryClass = dm.getDictionaryByCode(_code);
			
			SDictionaryDetail sdd = new SDictionaryDetail();
			sdd.setValue(_newvalue);
			sdd.setSort(-1);
			sdd.setSysFlag('N');
			sdd.setSDictionaryClass(sDictionaryClass);
			
			this.dictionaryDetailDAO.saveDictionaryDetails(sdd);
			
			List list = this.dictionaryDetailDAO.findDictDetailByCodes(-1);
			
			for( Iterator it = list.iterator(); it.hasNext();)
			{
				SDictionaryDetail sd = (SDictionaryDetail)it.next();
				
				sd.setSort(sd.getId());
				
				this.dictionaryDetailDAO.updateDictionaryDetails(sd);
			}
		}
	}
	
	@Override
	public Object findUpSort(Integer sort, String code)
	{
		return this.dictionaryDetailDAO.findUpSort(sort, code);
	}
	
	@Override
	public Object findDownSort(Integer sort, String code)
	{
		return this.dictionaryDetailDAO.findDownSort(sort, code);
	}
	
	public static void main(String [] args)
	{
		String tmp = "149@$^华东44^#$%@148@$^华南^#$%@147@$^华北^#$%@146@$^华中";
		
		String tmp2 = "149%sdfd";
		
		System.out.println(tmp2.split("%").length);
	}


	@Override
	public Object findDictDetailByCode(Integer code)
	{
		return this.dictionaryDetailDAO.findDictDetailByCode(code);
	}


	@Override
	public void updateSort(SDictionaryDetail dictionaryDetail)
	{
		this.dictionaryDetailDAO.updateSort(dictionaryDetail);
	}


	@Override
	public void updateSorts(SDictionaryDetail highDictionaryDetail,
			SDictionaryDetail lowDictionaryDetail)
	{
		this.dictionaryDetailDAO.updateSort(highDictionaryDetail);
		this.dictionaryDetailDAO.updateSort(lowDictionaryDetail);
	}


	@Override
	public void delDictionaryDetailsById(String id)
	{
		this.dictionaryDetailDAO.delDictionaryDetailsById(id);
	}

}
